<?php

//评论模块
class Comment_model extends Model {

	function Comment_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("accounts_model", "am");
	}
	
	//设置回应提醒已读
	function setread($dataid)
	{
		$data = array(
			'isread' => 1,
		);
		$this->db->where("id", $dataid);
		$result = $this->db->update("message_comment", $data);
		return $result;
	}
	
	//删除选择回应提醒
	function delselected_comment($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("message_comment");
		return $result;
	}
	
	//删除所有回应提醒
	function deleteall_comment($userid)
	{
		if($userid == '0') return false;
		$this->db->where("userid", $userid);
		$result = $this->db->delete("message_comment");
		return $result;
	}
	
	//获取收件箱信息总数
	function count_list_comment($userid)
	{
		$query = $this->db->get_where("message_comment", array("userid"=>$userid));
		return $query->num_rows();
	}
	
	//获取新回应信息
	function lists_comment($userid, $limit = 2, $offset = 1)
	{
		$lists = array();
		$query = $this->db->from("message_comment")->where("userid",$userid)->limit($limit, $offset)->order_by("id","desc")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row){
				$fromid = $row["fromid"];
				$title = $row["title"];
				preg_match('/href="(.*)">(.*)<\/a>/i', $title, $matches);
				$link = $showtitle = '';
				if(!empty($matches[1])) {
					$link = $matches[1];
				}
				if(!empty($matches[2])) {
					$showtitle = $matches[2];
				}
				$lists[$k]["link"] = $link;
				$lists[$k]["showtitle"] = $showtitle;
				if($fromid > 0) {
					$fromuser = $this->am->get($fromid);
					$fromusername = $fromuser["username"];
				} else {
					$fromusername = '热闹网';
				}
				$lists[$k]["fromuser"] = $fromusername;
			}
		}
		return $lists;
	}
	
	//检测是否有新回应
	function has_newcomment($userid)
	{
		$query = $this->db->get_where("message_comment", array("userid"=>$userid, "isread"=>0));
		if ($query->num_rows()>0) {
			return true;
		}
		return false;
	}
	
	//添加回应提醒
	function add_message($userid, $title)
	{
		$data = array(
			'fromid' => $this->userid,
			'userid' => $userid,
			'dataid' => p("dataid"),
			'title' => $title,
			'addtime' => time(),
		);
		$this->db->insert("message_comment", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//添加
	function add()
	{
		$data = array(
			'userid' => $this->userid,
			'siteid' => p("siteid"),
			'plid' => p("plid"),
			'dataid' => p("dataid"),
			'content' => p("content"),
			'addtime' => time(),
		);
		$this->db->insert("comment", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//删除
	function delete($dataid)
	{
		$this->db->where("id", $dataid);
		$result = $this->db->delete("comment");
		return $result;
	}
	
	//评论删除
	function delete_selected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("comment");
		return $result;
	}
	
	//获取
	function get($dataid)
	{
		$row = array();
		$query = $this->db->get_where("comment", array("id"=>$dataid));
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
	//获取
	function forum_get($dataid)
	{
		$row = array();
		$query = $this->db->get_where("module_forum_comment", array("id"=>$dataid));
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
	//获取
	function miniblog_get($dataid)
	{
		$row = array();
		$query = $this->db->get_where("module_miniblog_comment", array("id"=>$dataid));
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
	//获取
	function special_get($dataid)
	{
		$row = array();
		$query = $this->db->get_where("module_special_comment", array("id"=>$dataid));
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
	//列表
	function lists($siteid, $plid, $dataid, $limit = 20, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("comment")->where(array("siteid"=>$siteid, "plid"=>$plid, "dataid"=>$dataid))->order_by("id", "desc")->limit($limit, $offset)->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $list){
				$lists[$k]['userinfo'] = $this->am->get($list['userid']);
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($siteid, $plid, $dataid)
	{
		$query = $this->db->from("comment")->where(array("siteid"=>$siteid, "plid"=>$plid, "dataid"=>$dataid))->get();
		return $query->num_rows();
	}
	
	//社区评论添加
	//处理JS标签
	function forum_add()
	{
		$time = time();
		$data = array(
			'userid' => $this->userid,
			'siteid' => p("siteid"),
			'dataid' => p("dataid"),
			'content' => p("content"),
			'addtime' => $time,
		);
		$this->db->insert("module_forum_comment", $data);
		$insert_id = $this->db->insert_id();
		//更新评论最新时间
		$data = array(
			"commentime" => $time,
		);
		$this->db->where("id", p("dataid"));
		$this->db->update("module_forum", $data);
		return $insert_id;
	}
	
	//社区评论删除
	function forum_delete($dataid)
	{
		$this->db->where("id", $dataid);
		$result = $this->db->delete("module_forum_comment");
		return $result;
	}
	
	//社区评论删除
	function forum_delete_selected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_forum_comment");
		return $result;
	}
	
	//社区评论列表
	function forum_lists($siteid, $dataid, $limit = 30, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_forum_comment")->where(array("siteid"=>$siteid, "dataid"=>$dataid))->order_by("id", "desc")->limit($limit, $offset)->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $list){
				$lists[$k]['userinfo'] = $this->am->get($list['userid']);
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_forum_lists($siteid, $dataid)
	{
		$query = $this->db->from("module_forum_comment")->where(array("siteid"=>$siteid, "dataid"=>$dataid))->get();
		return $query->num_rows();
	}
	
	//广播评论添加
	function miniblog_add()
	{
		$data = array(
			'userid' => $this->userid,
			'siteid' => p("siteid"),
			'dataid' => p("dataid"),
			'content' => p("content"),
			'addtime' => time(),
		);
		$this->db->insert("module_miniblog_comment", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//社区评论删除
	function miniblog_delete($dataid)
	{
		$this->db->where("id", $dataid);
		$result = $this->db->delete("module_miniblog_comment");
		return $result;
	}
	
	//广播评论删除
	function miniblog_delete_selected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_miniblog_comment");
		return $result;
	}
	
	//广播评论列表
	function miniblog_lists($siteid, $dataid, $limit = 30, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_miniblog_comment")->where(array("siteid"=>$siteid, "dataid"=>$dataid))->order_by("id", "desc")->limit($limit, $offset)->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $list){
				$lists[$k]['userinfo'] = $this->am->get($list['userid']);
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_miniblog_lists($siteid, $dataid)
	{
		$query = $this->db->from("module_miniblog_comment")->where(array("siteid"=>$siteid, "dataid"=>$dataid))->get();
		return $query->num_rows();
	}
	
	//专题评论添加
	function special_add()
	{
		$data = array(
			'userid' => $this->userid,
			'siteid' => p("siteid"),
			'plid' => p("plid"),
			'dataid' => p("dataid"),
			'content' => p("content"),
			'typeid' => p("typeid"),
			'addtime' => time(),
		);
		$this->db->insert("module_special_comment", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//专题评论删除
	function special_delete($dataid)
	{
		$this->db->where("id", $dataid);
		$result = $this->db->delete("module_special_comment");
		return $result;
	}
	
	//专题评论删除
	function special_delete_selected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_special_comment");
		return $result;
	}
	
	//专题评论列表
	function special_lists($siteid, $plid, $dataid, $limit = 20, $offset = 0, $typeid = 0)
	{
		$lists = array();
		$query = $this->db->from("module_special_comment")->where(array("siteid"=>$siteid, "plid"=>$plid, "dataid"=>$dataid, "typeid"=>$typeid))->order_by("id", "desc")->limit($limit, $offset)->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $list){
				$lists[$k]['userinfo'] = $this->am->get($list['userid']);
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_special_lists($siteid, $plid, $dataid)
	{
		$count1 = $this->count_special_lists1($siteid, $plid, $dataid);
		$count2 = $this->count_special_lists2($siteid, $plid, $dataid);
		if ($count1 > $count2) {
			return $count1;
		}
		return $count2;
	}
	
	function count_special_lists1($siteid, $plid, $dataid)
	{
		$query = $this->db->from("module_special_comment")->where(array("siteid"=>$siteid, "plid"=>$plid, "dataid"=>$dataid, "typeid"=>"0"))->get();
		return $query->num_rows();
	}
	
	function count_special_lists2($siteid, $plid, $dataid)
	{$query = $this->db->from("module_special_comment")->where(array("siteid"=>$siteid, "plid"=>$plid, "dataid"=>$dataid, "typeid"=>"1"))->get();
		return $query->num_rows();
	}
	
}

/* End of file */